using System;
using System.Globalization;
using System.Text;
using System.Text.RegularExpressions;
namespace Microsoft.Phone.Controls
{
	internal static class DateTimeFormatHelper
	{
		private const double Hour = 60.0;
		private const double Day = 1440.0;
		private const string SingleMeridiemDesignator = "t";
		private const string DoubleMeridiemDesignator = "tt";
		private static DateTimeFormatInfo formatInfo_GetSuperShortTime = null;
		private static DateTimeFormatInfo formatInfo_GetMonthAndDay = null;
		private static DateTimeFormatInfo formatInfo_GetShortTime = null;
		private static object lock_GetSuperShortTime = new object();
		private static object lock_GetMonthAndDay = new object();
		private static object lock_GetShortTime = new object();
		private static readonly Regex rxMonthAndDay = new Regex("(d{1,2}[^A-Za-z]M{1,3})|(M{1,3}[^A-Za-z]d{1,2})");
		private static readonly Regex rxSeconds = new Regex("([^A-Za-z]s{1,2})");
		public static int GetRelativeDayOfWeek(DateTime dt)
		{
			return (dt.get_DayOfWeek() - CultureInfo.get_CurrentCulture().get_DateTimeFormat().get_FirstDayOfWeek() + 7) % 7;
		}
		public static bool IsFutureDateTime(DateTime relative, DateTime given)
		{
			return relative < given;
		}
		public static bool IsAnOlderYear(DateTime relative, DateTime given)
		{
			return relative.get_Year() > given.get_Year();
		}
		public static bool IsAnOlderWeek(DateTime relative, DateTime given)
		{
			return DateTimeFormatHelper.IsAtLeastOneWeekOld(relative, given) || DateTimeFormatHelper.GetRelativeDayOfWeek(given) > DateTimeFormatHelper.GetRelativeDayOfWeek(relative);
		}
		public static bool IsAtLeastOneWeekOld(DateTime relative, DateTime given)
		{
			return (double)((int)(relative - given).get_TotalMinutes()) >= 10080.0;
		}
		public static bool IsPastDayOfWeek(DateTime relative, DateTime given)
		{
			return DateTimeFormatHelper.GetRelativeDayOfWeek(relative) > DateTimeFormatHelper.GetRelativeDayOfWeek(given);
		}
		public static bool IsPastDayOfWeekWithWindow(DateTime relative, DateTime given)
		{
			return DateTimeFormatHelper.IsPastDayOfWeek(relative, given) && (double)((int)(relative - given).get_TotalMinutes()) > 180.0;
		}
		public static bool IsCurrentCultureJapanese()
		{
			return CultureInfo.get_CurrentCulture().get_Name().StartsWith("ja", 5);
		}
		public static bool IsCurrentCultureKorean()
		{
			return CultureInfo.get_CurrentCulture().get_Name().StartsWith("ko", 5);
		}
		public static bool IsCurrentCultureTurkish()
		{
			return CultureInfo.get_CurrentCulture().get_Name().StartsWith("tr", 5);
		}
		public static bool IsCurrentCultureHungarian()
		{
			return CultureInfo.get_CurrentCulture().get_Name().StartsWith("hu", 5);
		}
		public static bool IsCurrentUICultureFrench()
		{
			return CultureInfo.get_CurrentUICulture().get_Name().Equals("fr-FR", 4);
		}
		public static string GetAbbreviatedDay(DateTime dt)
		{
			if (!DateTimeFormatHelper.IsCurrentCultureJapanese() && !DateTimeFormatHelper.IsCurrentCultureKorean())
			{
				return dt.ToString("ddd", CultureInfo.get_CurrentCulture());
			}
			return "(" + dt.ToString("ddd", CultureInfo.get_CurrentCulture()) + ")";
		}
		public static string GetSuperShortTime(DateTime dt)
		{
			if (DateTimeFormatHelper.formatInfo_GetSuperShortTime == null)
			{
				lock (DateTimeFormatHelper.lock_GetSuperShortTime)
				{
					StringBuilder stringBuilder = new StringBuilder(string.Empty);
					DateTimeFormatHelper.formatInfo_GetSuperShortTime = (DateTimeFormatInfo)CultureInfo.get_CurrentCulture().get_DateTimeFormat().Clone();
					stringBuilder.Append(DateTimeFormatHelper.formatInfo_GetSuperShortTime.get_LongTimePattern());
					string value = DateTimeFormatHelper.rxSeconds.Match(stringBuilder.ToString()).get_Value();
					stringBuilder.Replace(" ", string.Empty);
					stringBuilder.Replace(value, string.Empty);
					if (!DateTimeFormatHelper.IsCurrentCultureJapanese() && !DateTimeFormatHelper.IsCurrentCultureKorean() && !DateTimeFormatHelper.IsCurrentCultureHungarian())
					{
						stringBuilder.Replace("tt", "t");
					}
					DateTimeFormatHelper.formatInfo_GetSuperShortTime.set_ShortTimePattern(stringBuilder.ToString());
				}
			}
			return dt.ToString("t", DateTimeFormatHelper.formatInfo_GetSuperShortTime).ToLowerInvariant();
		}
		public static string GetMonthAndDay(DateTime dt)
		{
			if (DateTimeFormatHelper.formatInfo_GetMonthAndDay == null)
			{
				lock (DateTimeFormatHelper.lock_GetMonthAndDay)
				{
					StringBuilder stringBuilder = new StringBuilder(string.Empty);
					DateTimeFormatHelper.formatInfo_GetMonthAndDay = (DateTimeFormatInfo)CultureInfo.get_CurrentCulture().get_DateTimeFormat().Clone();
					stringBuilder.Append(DateTimeFormatHelper.rxMonthAndDay.Match(DateTimeFormatHelper.formatInfo_GetMonthAndDay.get_ShortDatePattern()).get_Value());
					if (stringBuilder.ToString().Contains("."))
					{
						stringBuilder.Append(".");
					}
					DateTimeFormatHelper.formatInfo_GetMonthAndDay.set_ShortDatePattern(stringBuilder.ToString());
				}
			}
			return dt.ToString("d", DateTimeFormatHelper.formatInfo_GetMonthAndDay);
		}
		public static string GetShortDate(DateTime dt)
		{
			return dt.ToString("d", CultureInfo.get_CurrentCulture());
		}
		public static string GetShortTime(DateTime dt)
		{
			if (DateTimeFormatHelper.formatInfo_GetShortTime == null)
			{
				lock (DateTimeFormatHelper.lock_GetShortTime)
				{
					StringBuilder stringBuilder = new StringBuilder(string.Empty);
					DateTimeFormatHelper.formatInfo_GetShortTime = (DateTimeFormatInfo)CultureInfo.get_CurrentCulture().get_DateTimeFormat().Clone();
					stringBuilder.Append(DateTimeFormatHelper.formatInfo_GetSuperShortTime.get_LongTimePattern());
					string value = DateTimeFormatHelper.rxSeconds.Match(stringBuilder.ToString()).get_Value();
					stringBuilder.Replace(value, string.Empty);
					DateTimeFormatHelper.formatInfo_GetShortTime.set_ShortTimePattern(stringBuilder.ToString());
				}
			}
			return dt.ToString("t", DateTimeFormatHelper.formatInfo_GetShortTime);
		}
	}
}
